<template>
  <div>
    <el-dialog
      :title="ruleForm.id ? '编辑部门' : '新增部门'"
      :visible="visible"
      @close="close('ruleForm')"
    >
      <el-form
        ref="ruleFormRef"
        :rules="rules"
        :model="ruleForm"
        label-width="120px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="ruleForm.code" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="ruleForm.managerId" placeholder="请选择活动区域">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="ruleForm.introduce"
            placeholder="1-100个字符"
            type="textarea"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">
            {{ ruleForm.id ? '编辑' : '确定' }}
          </el-button>
          <el-button @click="close('ruleForm')"> 取消 </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getUserSimpleRequest, addDepartmentRequest, getDepartmentByIdRequest, updateDepartmentByIdRequest } from '@/api/department'

export default {
  name: 'AddDept',
  props: {
    visible: {
      type: Boolean

    },
    originList: {
      type: Array,
      default: () => []
    },
    currentId: {
      type: Number,
      default: 0
    }
  },
  data () {
    const validatorName = async (_, values, cb) => {
      // const res = await getDepartmentRequest()
      // console.log(res, values)
      let res = []
      if (this.ruleForm.id) {
        res = this.originList.filter(item => item.id !== this.currentId)
      } else {
        res = this.originList
      }
      if (res.some(item => item.name === values)) {
        cb(new Error('部门名称重复,请重新输入'))
      } else {
        cb()
      }

    }

    const validatorCode = async (_, values, cb) => {
      // const res = await getDepartmentRequest()
      // console.log(res, values)
      let res = []
      if (this.ruleForm.id) {
        res = this.originList.filter(item => item.id !== this.currentId)
      } else {
        res = this.originList
      }
      if (res.some(item => item.code === values)) {
        cb(new Error('部门编码重复,请重新输入'))
      } else {
        cb()
      }

    }
    return {
      // dialogVisible: false
      ruleForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: validatorName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: validatorCode, trigger: 'blur' }

        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' },
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }
        ],
      },
      //部门负责人列表
      managerList: []
    }
  },
  computed: {
  },
  created () {
    this.getUserSimple()
  },
  methods: {
    //部门负责人列表
    async getUserSimple () {
      const res = await getUserSimpleRequest()
      this.managerList = res.data
    },
    open () {
      this.dialogVisible = true
    },
    submitForm (formName) {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            //编辑
            await updateDepartmentByIdRequest(this.ruleForm)
          } else {
            //新增
            const res = await addDepartmentRequest({ ...this.ruleForm, pid: this.currentId })
          }

          //表单重置
          this.close()
          //通知父亲重新调用一下tree接口刷新页面
          this.$emit('updateParent')
        }
      });
    },
    resetForm (formName) {
      //重置表单 elm 里面的 只能清空校验的
      this.$refs.ruleFormRef.resetFields();
      //清空 表单 恢复到原4样
      this.ruleForm = this.$options.data().ruleForm
      //关闭弹出框
      this.$emit('update:visible', false)

    },
    close (formName) {
      this.resetForm(formName)
    },
    //获取-部门详情 - 编辑相关
    async getDepartmentById () {
      const res = await getDepartmentByIdRequest(this.currentId)
      this.ruleForm = res.data
    }
  }
}
</script>
<style lang='scss'  scoped>
</style>
